<?php
App::uses('AppController', 'Controller');
/**
 * Suppliers Controller
 *
 * @property Supplier $Supplier
 */
class SuppliersController extends AppController {
    
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Supplier->recursive = 0;
		$suppliers = $this->Supplier->find('all', array('conditions' => array('Supplier.archive' => '0')));
		$this->set(compact('suppliers'));
	}
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Supplier->exists($id)) {
			throw new NotFoundException(__('Invalid supplier'));
		}
		$options = array('conditions' => array('Supplier.' . $this->Supplier->primaryKey => $id));
		$this->set('supplier', $this->Supplier->find('first', $options));

		$this->loadModel('Productsbrands');
		$brands = $this->Productsbrands->find('all');
		$brand = array();
		foreach($brands as $i=>$j){
			$brand = Set::insert($brand, $j['Productsbrands']['id'], $j['Productsbrands']['name']);
		}

		$this->loadModel('Productsranges');
		$ranges = $this->Productsranges->find('all');
		$range = array();
		foreach($ranges as $i=>$j){
			$range = Set::insert($range, $j['Productsranges']['id'], $j['Productsranges']['name']);
		}

		$this->loadModel('Productscategories');
		$categories = $this->Productscategories->find('all');
		$category = array();
		foreach($categories as $i=>$j){
			$category = Set::insert($category, $j['Productscategories']['id'], $j['Productscategories']['name']);
		}

		$this->set(compact('brand', 'range', 'category'));
                
                  
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Supplier->create();
			if ($this->Supplier->save($this->request->data)) {
				$this->Session->setFlash(__('The supplier has been saved'),'default', array('class' => 'savesuccess'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The supplier could not be saved. Please, try again.'),'default', array('class' => 'saveerror'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Supplier->exists($id)) {
			throw new NotFoundException(__('Invalid supplier'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Supplier->save($this->request->data)) {
				$this->Session->setFlash(__('The supplier has been saved'),'default', array('class' => 'savesuccess'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The supplier could not be saved. Please, try again.'),'default', array('class' => 'saveerror'));
			}
		} else {
			$options = array('conditions' => array('Supplier.' . $this->Supplier->primaryKey => $id));
			$this->request->data = $this->Supplier->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Supplier->id = $id;
		if (!$this->Supplier->exists()) {
			throw new NotFoundException(__('Invalid supplier'));
		}
		$this->request->onlyAllow('post', 'delete');
		$supplier = $this->Supplier->findById($id);
		$supplier = $supplier['Supplier'];
		$supplier = Set::insert($supplier, 'archive', '1');
		if ($this->Supplier->save($supplier)){

			//print_r('product');
			$product = $this->Supplier->Product->find('list', array('conditions'=>array('suppliers_id'=>$id)));
			$supplier = $this->Supplier->query('SELECT id FROM Suppliers WHERE supplierName = "No Supplier"');
			$nosupid = $supplier['0']['Suppliers']['id'];
			foreach($product as $j){
				$data = array('id' => $j, 'suppliers_id' => $nosupid);
				$this->Supplier->Product->save($data);
			}

			//print_r('order');
			$order = $this->Supplier->Order->find('list', array('conditions'=>array('suppliers_id'=>$id)));
			foreach ($order as $j) {

				//archive delivery
				$this->loadModel('Delivery');
				$delivery = $this->Delivery->find('list', array('conditions'=>array('orders_id'=>$j)));
				foreach ($delivery as $i) {
					$data = array('id' => $i, 'archive' => '1');
					$this->Delivery->save($data);
				}

				//archive ordersproduct
				$this->loadModel('Ordersproduct');
				$this->loadModel('Deliveriesordersproduct');
				$op = $this->Ordersproduct->find('list', array('conditions'=>array('orders_id'=>$id)));
				foreach ($op as $l) {

					//archive deliveriesordersproduct
					$dop = $this->Deliveriesordersproduct->find('list', array('conditions'=>array('ordersproducts_id'=>$l)));
					foreach ($dop as $k) {
						$data = array('id' => $k, 'archive' => '1');
						//$this->Deliveriesordersproduct->save($data);
					}

					$data = array('id' => $l, 'archive' => '1');
					$this->Ordersproduct->save($data);
				}

				//archive order
				$data = array('id' => $j, 'archive' => '1');
				$this->Supplier->Order->save($data);
			}

			$this->Session->setFlash(__('Supplier deleted'),'default', array('class' => 'savesuccess'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Supplier was not deleted'),'default', array('class' => 'saveerror'));
		$this->redirect(array('action' => 'index'));
	}
}
